package flyshoes.controller;

import flyshoes.mybatis.entity.Admin;
import flyshoes.mybatis.entity.Product;
import flyshoes.mybatis.entity.Result;
import flyshoes.service.AdministerServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminServiceController {
    private final AdministerServiceImpl adminService;
    @PostMapping("/products/{categoryName}")  //新增商品
    public Result addProduct(@RequestBody Product product, @PathVariable String categoryName){
        Integer categoryId = adminService.getCategoryIdByName(categoryName);

        if (categoryId == null) {
            return Result.fail(400, "分类不存在");
        }
        product.setCategoryId(categoryId);
        return adminService.addProduct(product);
    }
    @PostMapping("/import")
    public Result<String> uploadProducts(MultipartFile[] files) {
        try {
            for (MultipartFile file : files) {
                adminService.uploadProducts(file);
            }
            return Result.success("成功");
        } catch (IOException e) {
            return Result.fail(e.getLocalizedMessage());
        }
    }
    @GetMapping("/userDetailInfo") //得到用户详细信息
    public Result userDetailInfo(){
        return adminService.userDetailInfo();
    }
    @GetMapping("/order")  //得到订单信息
    public Result order(){
        return adminService.order();
    }
    @GetMapping("/product") //得到商品信息
    public Result product(){
        return adminService.product();
    }
    @GetMapping("/shipInfo") //得到地址信息
    public Result shipInfo(){
        return adminService.shipInfo();
    }
    @PostMapping("/login")//登录
    public Result login(@RequestBody Map admin){
        return adminService.adminLogin(admin);
    }
    @PostMapping("/register")//注册
    public Result<Admin> register(@RequestBody Admin admin){
        return adminService.adminregister(admin);
    }
    @DeleteMapping("/delproduct")
    public Result delProduct(@RequestBody Product product){
        return adminService.DeleteProduct(product);
    }
}
